﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: frmRegisterClass.cs
#endregion

using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.finan.entries
{
	/// <summary>
	/// Description of frmRegisterGroup.
	/// </summary>
	public partial class frmRegisterBill : csharp.common.RegisterTemplate
	{
		private Bill bill;
		private Bills listBills;
		//private SubClasss listSubClass;
		//private Classs listClass;
		private SubGroups listSubgroups;
		private Groups    listGroups;
		
		private bool mutexGroup;
		//private bool mutexSubGroup;
		//private bool mutexClass;
		
		public frmRegisterBill()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			bill          = new Bill();
			listBills     = new Bills();
			//listSubClass  = new SubClasss();
			//listClass     = new Classs();
			listSubgroups = new SubGroups();
			listGroups    = new Groups();
		}
		
		protected override void newEvent()
		{
			bill.clear();
			txtCode.Focus();
		}
		
		/// <summary>
		/// Set data from edits to class
		/// </summary>
		protected override void setClass()
		{
			bill.code= txtCode.Text;
			bill.name= txtGroup.Text;
			bill.active= cbxActive.Checked;
			bill.subgroupId= (cbxSubGroup.SelectedItem as SubGroup).id; //listSubClass.get(cbxSubClass.SelectedIndex).id;
		}
		
		/// <summary>
		/// Set data from class to edits
		/// </summary>
		protected override void setEdits()
		{
			bill.id= dgvMain.CurrentRow.Cells["id"].Value.ToString();
			
			if (bill.get())
			{
				txtCode.Text= bill.code;
				txtGroup.Text= bill.name;
				cbxActive.Checked= bill.active;
				
				Functions.setSelectIndexComboSubGroup(cbxSubGroup, bill.subgroupId);
				//listSubClass.search(bill.subclassId);
				//cbxSubClass.SelectedIndex = listSubClass.getIndex();
				
				//listClass.search(listSubClass.get().classId);
				//cbxClass.SelectedIndex = listClass.getIndex();
				
				//listSubgroups.search(listClass.get().subGroupId);
				//cbxSubGroup.SelectedIndex= listSubgroups.getIndex();
				
				//listGroups.search(listSubgroups.get().groupId);
				//cbxGroup.SelectedIndex= listGroups.getIndex();
			}
		}
		
		/// <summary>
		/// Fill the DataGridView
		/// </summary>
		protected override void fillDataGridView()
		{
			int tam;		
			
			dgvMain.Rows.Clear();
			tam= listBills.count();
			for (int i=0; i < tam; i++)
			{
				String []row= {listBills.get(i).id, listBills.get(i).code, listBills.get(i).name};
				dgvMain.Rows.Add(row);
			}

		}
		
		/// <summary>
		/// Find the object of search
		/// </summary>
		protected override void find()
		{
			common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
		}		
		
	   /// <summary>
	   /// Save the register in the database.
	   /// </summary>
	   /// <returns>bool - true if the register was saved sucessful, false case not.</returns>	
	   protected override bool save()
	   {
	   	if(bill.save())
	   	{
	   		//listBills.fillList((cbxSubGroup.SelectedItem as SubGroup).id);
	   		listBills.fillList();
	   		fillDataGridView();
	   		
	   		return true;
	   	}
	   	return false;
	   }
	   
	   /// <summary>
	   /// Update the register in the database
	   /// </summary>
	   /// <returns>bool - true if the register was updated sucessful, false case not.</returns>
	   protected override bool update()
	   {   
	   	if (bill.update())
	   	{
	   		//listBills.fillList((cbxSubGroup.SelectedItem as SubGroup).id);
	   		listBills.fillList();
	   		fillDataGridView();
	   		
	   		return true;
	   	}
	   	return false;	   		
	   }
	   
	   /// <summary>
	   /// Delete the register in the database.
	   /// </summary>
	   /// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
	   protected override bool delete()
	   {
	   	bill.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
	   	if (bill.delete())
	   	{
	   		listBills.fillList();
	   		//fillDataGridView();
	   		
	   		return true;
	   	}
	   	return false;	   		
	   }
	   
	   /// <summary>
	   /// Method call in initialization
	   /// </summary>
	   protected override void init()
	   {
	   	KryptonDataGridViewTextBoxColumn id   = new KryptonDataGridViewTextBoxColumn();
	   	KryptonDataGridViewTextBoxColumn code = new KryptonDataGridViewTextBoxColumn();
	   	KryptonDataGridViewTextBoxColumn name = new KryptonDataGridViewTextBoxColumn();
	   	
	   	dgvMain.Columns.AddRange(new DataGridViewColumn[] {id,code,name});
	   	
	   	id.Name= "id";
	   	id.Visible= false;
	   	
	   	code.Name = "code";
	   	code.HeaderText= "Código";
	   	
	   	name.Name= "name";
	   	name.Width= 200;
	   	name.HeaderText = "Nome";
	   	
	   	listBills.fillList();
	   	
	   	//Fill groups
	   	listGroups.fillList();
	   	Functions.fillComboGroups(listGroups, cbxGroup, "id", "name");
	   	
	   	//Fill subgroups
	   	listSubgroups.fillList();
	   	Functions.fillComboSubGroups(listSubgroups, cbxSubGroup, "id", "name");
	   	
	   	//Fill classes
	   	//listClass.fillList();
	   	//Functions.fillComboClass(listClass, cbxClass, "id", "name");
	   	
	   	//Fill subclasses
	   	//listSubClass.fillList();
	   	//Functions.fillComboSubClass(listSubClass, cbxSubClass, "id", "name");
	   }		
		
		private void TabPrincipalEnter(object sender, EventArgs e)
		{
			txtCode.Focus();
		}
		
		void CbxGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if (cbxGroup.SelectedIndex >= 0)
			{
				if (!mutexGroup)
				{
					listSubgroups.filterByGroup((cbxGroup.SelectedItem as Group).id);
					
					Functions.fillComboSubGroups(listSubgroups, cbxSubGroup, "id", "name");
					
					listSubgroups.disableFilter();
				}
			}
		}
		
		void CbxSubGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if (cbxSubGroup.SelectedIndex >= 0)
			{
			   /*if (!mutexSubGroup)
				{
					listClass.filterBySubGroup((cbxSubGroup.SelectedItem as SubGroup).id);
					
					Functions.fillComboClass(listClass, cbxClass, "id", "name");
					
					listClass.disableFilter();
				}*/
				
				//listBills.filterSubGroup((cbxSubGroup.SelectedItem as SubGroup).id);
				//fillDataGridView();
				
				if (txtCode.Text.Equals(String.Empty))
				{
					int aux = listBills.count() + 1;
					
					if (aux >= 100)
						txtCode.Text = aux.ToString();
					else if (aux >= 10)
						txtCode.Text = "0"+aux;
					else
						txtCode.Text = "00"+aux;
				}
			   //listBills.disableFilter();		   
				mutexGroup = true;
				Functions.setSelectIndexComboGroup(cbxGroup, (cbxSubGroup.SelectedItem as SubGroup).groupId);
				mutexGroup = false;
			}
		}
		
		#region: Old code
		/*void CbxClassSelectedValueChanged(object sender, EventArgs e)
		{
			if (cbxClass.SelectedIndex >= 0)
			{
				if (!mutexClass)
				{
					listSubClass.filterByClass((cbxClass.SelectedItem as Class).id);
					
					Functions.fillComboSubClass(listSubClass, cbxSubClass, "id", "name");
					
					listSubClass.disableFilter();
				}
				
				mutexSubGroup = true;
				Functions.setSelectIndexComboSubGroup(cbxSubGroup, (cbxClass.SelectedItem as Class).subGroupId);
				mutexSubGroup = false;
			}			
		}
		
		void CbxSubClassSelectedValueChanged(object sender, EventArgs e)
		{
			if (cbxSubClass.SelectedIndex >= 0)
			{
				mutexClass = true;
				Functions.setSelectIndexComboClass(cbxClass, (cbxSubClass.SelectedItem as SubClass).classId);
				mutexClass = false;
				
				listBills.filterSubClass((cbxSubClass.SelectedItem as SubClass).id);
				fillDataGridView();
				
				if (txtCode.Text.Equals(String.Empty))
				{
					int aux = listBills.count() + 1;
					
					if (aux >= 100)
						txtCode.Text = aux.ToString();
					else if (aux >= 10)
						txtCode.Text = "0"+aux;
					else
						txtCode.Text = "00"+aux;
				}
			   listBills.disableFilter();				
			}
		}
		*/
		#endregion
	}
}
